package com.xingongchang.baolebei.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.XListView;
import com.external.maxwin.view.XListView.IXListViewListener;
import com.insthub.BeeFramework.Utils.TimeUtil;
import com.insthub.BeeFramework.activity.BaseActivity;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.insthub.BeeFramework.view.ToastView;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.UMSsoHandler;
import com.xingongchang.baolebei.BaolebeiManager;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.activity.TopicDetailActivity.GoodsAdapter;
import com.xingongchang.baolebei.adapter.GoosItemAdapter;
import com.xingongchang.baolebei.adapter.ProductCommentAdapter;
import com.xingongchang.baolebei.adapter.ProductPhotoAdapter;
import com.xingongchang.baolebei.fragment.LvHeight;
import com.xingongchang.baolebei.model.CommentModel;
import com.xingongchang.baolebei.model.GoodDetailDraft;
import com.xingongchang.baolebei.model.GoodDetailModel;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.model.ShoppingCartModel;
import com.xingongchang.baolebei.protocol.COMMENTS;
import com.xingongchang.baolebei.protocol.SPECIFICATION;
import com.xingongchang.baolebei.protocol.SPECIFICATION_VALUE;
import com.xingongchang.baolebei.protocol.STATUS;

public class ProductDetailActivity extends BaseActivity implements
		BusinessResponse, IXListViewListener
{
	private ProductPhotoAdapter photoListAdapter;
	private GoodDetailModel dataModel;

	private TextView goodBriefTextView;
	private TextView goodPromotePriceTextView;
	private TextView goodMarketPriceTextView;
	private Button goodCategoryTextView;

	private LinearLayout goodsDetails;
	private LinearLayout goodsComments;
	GoosItemAdapter adapter;
	private TextView title;
	private ImageView back;
	private ImageView share;

	private ImageView good_img;

	private TextView addToCartTextView;
	private TextView buyNowTextView, xiaoliang;
	private Boolean isBuyNow = false;
	private ImageView collectionButton;
	private ImageView pager;

	private ImageView goodDetailShoppingCart;
	private TextView good_detail_shopping_cart_num;
	Boolean reload = true;
	private String commentStr = "";
	// private LinearLayout good_detail_shopping_cart_num_bg;

	private View headView;
	private XListView xlistView;

	private SharedPreferences shared;
	private SharedPreferences.Editor editor;
	// private Timer timer;
	private WebView webView, webView2;
	private CommentModel commentModel;
	private ListView mListView;
	private ProductCommentAdapter commentAdapter;
	private TextView filter_detail_tabone;
	private TextView filter_comment_tabtwo;
	FrameLayout fragment;
	private LinearLayout layout_recom;
	private GridView gridviews;
	public int screenwidth;
	private Button loadmore;
	private Boolean isComment = false;
	private Boolean istrue;
	private TextView play_name, play_text;
	NetworkInfo mNetworkInfo;// 判断网络是否连接
	private LinearLayout play_layout;
	NetworkInfo mobNetInfo;
	ConnectivityManager mConnectivityManager;
	NetworkInfo wifiNetInfo;
	// 首先在您的Activity中添加如下成员变量
	final UMSocialService mController = UMServiceFactory
			.getUMSocialService("com.umeng.share");

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_detail);
		shared = getSharedPreferences("userInfo", 0);
		editor = shared.edit();
		// 判断是否连接网络
		mConnectivityManager = (ConnectivityManager) ProductDetailActivity.this
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
		mobNetInfo = mConnectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		wifiNetInfo = mConnectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (mNetworkInfo == null)
		{
			ToastView toast = new ToastView(this, "网络异常，请检查网络设置");
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		}
		xlistView = (XListView) findViewById(R.id.good_detail_list);
		headView = LayoutInflater.from(this).inflate(
				R.layout.product_detail_head, null);
		layout_recom = (LinearLayout) headView.findViewById(R.id.layout_recom);
		play_name = (TextView) headView.findViewById(R.id.play_name);
		play_text = (TextView) headView.findViewById(R.id.play_text);
		play_layout = (LinearLayout) headView.findViewById(R.id.play_layout);
		fragment = (FrameLayout) headView.findViewById(R.id.frameent01);
		gridviews = (GridView) headView.findViewById(R.id.gridviews);
		xiaoliang = (TextView) headView.findViewById(R.id.xiaoliang);
		loadmore = (Button) headView.findViewById(R.id.loadmore);
		xlistView.addHeaderView(headView);
		xlistView.setPullLoadEnable(false);
		xlistView.setRefreshTime();
		xlistView.setXListViewListener(this, 1);
		xlistView.setAdapter(null);
		webView2 = (WebView) headView.findViewById(R.id.comment_lists);
		dataModel = new GoodDetailModel(this);
		dataModel.addResponseListener(this);
		dataModel.goodId = getIntent().getIntExtra("good_id", 0);
		dataModel.fetchGoodDetail(dataModel.goodId);
		showphone();
		commentModel = new CommentModel(this);
		commentModel.addResponseListener(this);

		final Resources resource = (Resources) getBaseContext().getResources();

		share = (ImageView) findViewById(R.id.top_view_share);

		share.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				// 设置分享内容
				mController
						.setShareContent("我在iBaby孕婴馆(http://baolebei.net/)上看到一件商品，感觉不错：【"
								+ dataModel.goodDetail.goods_name + "】");
				// 设置分享图片, 参数2为图片的url地址
				mController.setShareMedia(new UMImage(
						ProductDetailActivity.this,
						dataModel.goodDetail.img.thumb));
				mController.openShare(ProductDetailActivity.this, false);
			}
		});

		title = (TextView) findViewById(R.id.top_view_text);
		String det = resource.getString(R.string.gooddetail_product);
		title.setText(det);
		back = (ImageView) findViewById(R.id.top_view_back);
		back.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		good_img = (ImageView) headView.findViewById(R.id.good_img);
		goodBriefTextView = (TextView) headView.findViewById(R.id.good_brief);
		goodPromotePriceTextView = (TextView) headView
				.findViewById(R.id.promote_price);
		goodMarketPriceTextView = (TextView) headView
				.findViewById(R.id.market_price);
		goodMarketPriceTextView.getPaint()
				.setFlags(Paint.STRIKE_THRU_TEXT_FLAG);// 设置中划线
		webView = (WebView) headView.findViewById(R.id.gooddetails_web);
		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setBuiltInZoomControls(true);
		webSettings.setSupportZoom(true);
		webView.setVisibility(View.VISIBLE);
		goodCategoryTextView = (Button) headView
				.findViewById(R.id.good_category);
		goodCategoryTextView.setSingleLine(false);
		goodCategoryTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{

				for (int i = 0; i < dataModel.goodDetail.specification.size(); i++)
				{
					SPECIFICATION specification = dataModel.goodDetail.specification
							.get(i);
					if (null != specification.attr_type
							&& 0 == specification.attr_type
									.compareTo(SPECIFICATION.SINGLE_SELECT)
							&& false == GoodDetailDraft.getInstance()
									.isHasSpecName(specification.name))
					{
						SPECIFICATION_VALUE specification_value_one = specification.value
								.get(0);
						GoodDetailDraft.getInstance().addSelectedSpecification(
								specification_value_one);
					}
				}

				if (dataModel.goodDetail.goods_number != null)
				{
					GoodDetailDraft.getInstance().goodDetail = dataModel.goodDetail;
					Intent it = new Intent(ProductDetailActivity.this,
							SpecificationActivity.class);
					it.putExtra("num",
							Integer.valueOf(dataModel.goodDetail.goods_number));
					startActivity(it);
					overridePendingTransition(R.anim.my_scale_action,
							R.anim.my_alpha_action);

				} else
				{
					String che = resource.getString(R.string.check_the_network);
					ToastView toast = new ToastView(ProductDetailActivity.this,
							che);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}

			}
		});

		filter_detail_tabone = (TextView) headView
				.findViewById(R.id.filter_detail_tabone);
		filter_detail_tabone.setTextColor(getResources().getColor(
				R.color.title_select));
		filter_comment_tabtwo = (TextView) headView
				.findViewById(R.id.filter_comment_tabtwo);
		filter_comment_tabtwo.setTextColor(getResources().getColor(
				R.color.title_custom));
		goodsDetails = (LinearLayout) headView
				.findViewById(R.id.layout_good_details);
		goodsDetails.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				goodsDetails
						.setBackgroundResource(R.drawable.detail_tab_selected);
				goodsComments
						.setBackgroundResource(R.drawable.detail_tab_normal);
				filter_detail_tabone.setTextColor(getResources().getColor(
						R.color.title_select));
				filter_comment_tabtwo.setTextColor(getResources().getColor(
						R.color.title_custom));
				webView.setVisibility(View.VISIBLE);
				// mListView.setVisibility(View.GONE);
				webView2.setVisibility(View.GONE);
				fragment.setVisibility(View.GONE);
				layout_recom.setVisibility(View.VISIBLE);
				loadmore.setVisibility(View.GONE);
				isComment = false;
				showphone();
			}
		});
		loadmore.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				commentModel.getCommentsMore(dataModel.goodId);
			}
		});
		goodsComments = (LinearLayout) headView
				.findViewById(R.id.layout_good_comments);
		// goodsComments.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// goodsDetails
		// .setBackgroundResource(R.drawable.detail_tab_normal);
		// goodsComments
		// .setBackgroundResource(R.drawable.detail_tab_selected);
		// filter_detail_tabone.setTextColor(getResources().getColor(
		// R.color.title_custom));
		// filter_comment_tabtwo.setTextColor(getResources().getColor(
		// R.color.title_select));
		// webView.setVisibility(View.GONE);
		// mListView.setVisibility(View.VISIBLE);
		// goodCategoryTextView.setVisibility(View.GONE);
		// // fragment.setVisibility(View.GONE);
		// commentModel.getCommentList(dataModel.goodDetail.id);
		// }
		// });

		addToCartTextView = (TextView) findViewById(R.id.add_to_cart);
		addToCartTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				String uid = shared.getString("uid", "");
				if (uid.equals(""))
				{
					Intent intent = new Intent(ProductDetailActivity.this,
							SigninActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.push_buttom_in,
							R.anim.push_buttom_out);
					String nol = resource.getString(R.string.no_login);
					ToastView toast = new ToastView(ProductDetailActivity.this,
							nol);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else
				{
					isBuyNow = false;
					cartCreate();
				}
			}
		});
		buyNowTextView = (TextView) findViewById(R.id.buy_now);
		buyNowTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				String uid = shared.getString("uid", "");
				if (uid.equals(""))
				{
					Intent intent = new Intent(ProductDetailActivity.this,
							SigninActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.push_buttom_in,
							R.anim.push_buttom_out);
					String nol = resource.getString(R.string.no_login);
					ToastView toast = new ToastView(ProductDetailActivity.this,
							nol);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else
				{
					isBuyNow = true;
					cartCreate();

				}

			}
		});

		collectionButton = (ImageView) findViewById(R.id.collection_button);
		collectionButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				String uid = shared.getString("uid", "");
				if (uid.equals(""))
				{
					Intent intent = new Intent(ProductDetailActivity.this,
							SigninActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.push_buttom_in,
							R.anim.push_buttom_out);
					String nol = resource.getString(R.string.no_login);
					ToastView toast = new ToastView(ProductDetailActivity.this,
							nol);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else
				{
					if (dataModel.goodDetail.collected == 1)
					{
						ToastView toast = new ToastView(
								ProductDetailActivity.this,
								R.string.favorite_added);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					} else
					{
						dataModel.collectCreate(dataModel.goodId);
						collectionButton.setImageResource(R.drawable.liked_btn);
					}
				}

			}
		});

		goodDetailShoppingCart = (ImageView) findViewById(R.id.good_detail_shopping_cart);
		goodDetailShoppingCart.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				String uid = shared.getString("uid", "");
				if (uid.equals(""))
				{
					Intent intent = new Intent(ProductDetailActivity.this,
							SigninActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.push_buttom_in,
							R.anim.push_buttom_out);
					String nol = resource.getString(R.string.no_login);
					ToastView toast = new ToastView(ProductDetailActivity.this,
							nol);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else
				{
					Intent it = new Intent(ProductDetailActivity.this,
							ShoppingCartActivity.class);
					startActivityForResult(it, 1);
				}

			}
		});

		good_detail_shopping_cart_num = (TextView) findViewById(R.id.good_detail_shopping_cart_num);
		// good_detail_shopping_cart_num_bg = (LinearLayout)
		// findViewById(R.id.good_detail_shopping_cart_num_bg);

		pager = (ImageView) findViewById(R.id.good_detail_pager);
		pager.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
			}
		});
		mConnectivityManager = (ConnectivityManager)ProductDetailActivity.this
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
		if (mNetworkInfo != null)
		{
			// 设置购物车数量
			if (ShoppingCartModel.getInstance().goods_num == 0)
			{
				// good_detail_shopping_cart_num_bg.setVisibility(View.GONE);
				good_detail_shopping_cart_num.setVisibility(View.GONE);
			} else
			{
				// good_detail_shopping_cart_num_bg.setVisibility(View.VISIBLE);
				good_detail_shopping_cart_num.setVisibility(View.VISIBLE);
				good_detail_shopping_cart_num.setText(ShoppingCartModel
						.getInstance().goods_num + "");
			}
		}
	}

	// 显示推荐商品
	private void showphone()
	{
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenwidth = dm.widthPixels;
		if (dataModel.arrayList.size() > 0 && dataModel.arrayList != null)
		{
			if (isComment)
			{
				layout_recom.setVisibility(View.GONE);
			} else
			{
				layout_recom.setVisibility(View.VISIBLE);
			}
			// layout_recom.setVisibility(View.VISIBLE);
			int length = dataModel.arrayList.size();
			int gridviewWidth = (int) ((screenwidth * 2 / 5 + 10) * length + 10);
			int itemWidth = (int) (screenwidth * 2 / 5);
			// 根据屏幕密度和你的数据长度设置gridView的宽度，和每个item的宽度
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					gridviewWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
			gridviews.setLayoutParams(params);
			gridviews.setColumnWidth(itemWidth);
			gridviews.setHorizontalSpacing(10);
			gridviews.setStretchMode(GridView.NO_STRETCH);
			gridviews.setNumColumns(length);
			adapter = new GoosItemAdapter(this, dataModel.arrayList);
			gridviews.setAdapter(adapter);
			gridviews.setOnItemClickListener(new OnItemClickListener()
			{

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id)
				{
					// TODO Auto-generated method stub
					Intent it = new Intent(ProductDetailActivity.this,
							ProductDetailActivity.class);
					it.putExtra("good_id",
							dataModel.arrayList.get(position).goods_id);
					startActivity(it);
				}
			});

		} else
		{
			layout_recom.setVisibility(View.GONE);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		return super.onTouchEvent(event);
	}

	// 获取网络图片，返回bitmap
	public Bitmap getBitMap(String url)
	{
		Bitmap bitmap = null;
		URL picUrl = null;
		try
		{
			picUrl = new URL(url);
		} catch (MalformedURLException e)
		{
			// TODO: handle exception
			e.printStackTrace();
		}

		try
		{
			HttpURLConnection connection = (HttpURLConnection) picUrl
					.openConnection();
			connection.setDoInput(true);
			connection.connect();
			InputStream inputStream = connection.getInputStream();
			bitmap = BitmapFactory.decodeStream(inputStream);
			inputStream.close();
		} catch (IOException e)
		{
			// TODO: handle exception
			e.printStackTrace();
		}

		return bitmap;

	}

	void cartCreate()
	{
		ArrayList<Integer> specIdList = new ArrayList<Integer>();

		boolean isRedrectToSpecification = false;
		if (GoodDetailDraft.getInstance().selectedSpecification.size() == 0)
		{
			for (int i = 0; i < dataModel.goodDetail.specification.size(); i++)
			{
				SPECIFICATION specification = dataModel.goodDetail.specification
						.get(i);
				if (null != specification.attr_type
						&& 0 == specification.attr_type
								.compareTo(SPECIFICATION.SINGLE_SELECT))
				{
					SPECIFICATION_VALUE specification_value_one = specification.value
							.get(0);
					GoodDetailDraft.getInstance().addSelectedSpecification(
							specification_value_one);
					isRedrectToSpecification = true;
				}
			}

			if (isRedrectToSpecification)
			{
				if (dataModel.goodDetail.goods_number != null)
				{
					ToastView toast = new ToastView(this,
							R.string.select_specification_first);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();

					GoodDetailDraft.getInstance().goodDetail = dataModel.goodDetail;
					Intent it = new Intent(this, SpecificationActivity.class);
					it.putExtra("num",
							Integer.valueOf(dataModel.goodDetail.goods_number));
					it.putExtra("creat_cart", true);
					startActivityForResult(it, 2);
					overridePendingTransition(R.anim.my_scale_action,
							R.anim.my_alpha_action);
				} else
				{
					Resources resource = (Resources) getBaseContext()
							.getResources();
					String che = resource.getString(R.string.check_the_network);
					ToastView toast = new ToastView(ProductDetailActivity.this,
							che);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}

				return;
			}
		}

		for (int i = 0; i < GoodDetailDraft.getInstance().selectedSpecification
				.size(); i++)
		{
			SPECIFICATION_VALUE specification_value = GoodDetailDraft
					.getInstance().selectedSpecification.get(i);
			specIdList.add(Integer.valueOf(specification_value.id));
		}

		dataModel.cartCreate(dataModel.goodId, specIdList,
				GoodDetailDraft.getInstance().goodQuantity);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if ((keyCode == KeyEvent.KEYCODE_BACK))
		{
			finish();
			overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			return false;
		}
		return true;
	}

	private Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			if (msg.what == 1)
			{
				Bitmap bitmap = (Bitmap) msg.obj;
				good_img.setImageBitmap(bitmap);
			}
		}
	};

	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException
	{
		xlistView.stopRefresh();
		Resources resource = (Resources) getBaseContext().getResources();
		if (url.endsWith(ProtocolConst.GOODSDETAIL))
		{
			if (dataModel.responseStatus.succeed == 1)
			{
				JSONObject object = jo.optJSONObject("data");
				istrue = object.optBoolean("transfree");
				String no = object.optString("transfree_activity");
				if (istrue == true)
				{
					play_layout.setVisibility(View.VISIBLE);
					play_name.setText(no);
					play_text.setVisibility(View.VISIBLE);
				}
				showphone();
				pager.setVisibility(View.GONE);
				xlistView.setRefreshTime();
				GoodDetailDraft.getInstance().clear();
				GoodDetailDraft.getInstance().goodDetail = dataModel.goodDetail;
				goodBriefTextView.setText(dataModel.goodDetail.goods_name);
				String sum = String.valueOf(dataModel.goodDetail.salesnum);
				xiaoliang.setText(sum);
				new Thread()
				{
					public void run()
					{
						Bitmap bitmap = getBitMap(dataModel.goodDetail.img.url);
						Message msg = new Message();
						msg.what = 1;
						msg.obj = bitmap;
						handler.sendMessage(msg);

					}
				}.start();
				String marketStr = resource.getString(R.string.market_price);
				goodPromotePriceTextView
						.setText(dataModel.goodDetail.formated_promote_price);
				goodMarketPriceTextView.setText(marketStr
						+ dataModel.goodDetail.market_price);

				if (dataModel.goodDetail.promote_price.equals("0"))
				{
					// contentString += "<br>促销价格：无</br>";
					goodPromotePriceTextView
							.setText(dataModel.goodDetail.shop_price);
				}

				if (dataModel.goodDetail.collected == 0)
				{
					collectionButton.setImageResource(R.drawable.like_btn);
				} else
				{
					collectionButton.setImageResource(R.drawable.liked_btn);
				}

				commentModel = new CommentModel(this);
				commentModel.addResponseListener(this);
				commentModel.getCommentList(dataModel.goodDetail.id);
				dataModel.goodsDesc(dataModel.goodDetail.id);
				goodCategoryTextView.setText(getSpecificationDesc());

				photoListAdapter.dataList = dataModel.goodDetail.pictures;

				// 设置购物车数量
				if (ShoppingCartModel.getInstance().goods_num == 0)
				{
					good_detail_shopping_cart_num.setVisibility(View.GONE);
				} else
				{
					good_detail_shopping_cart_num.setVisibility(View.VISIBLE);
					good_detail_shopping_cart_num.setText(ShoppingCartModel
							.getInstance().goods_num + "");
				}

			}

		} else if (url.endsWith(ProtocolConst.CARTCREATE))
		{
			STATUS responseStatus = STATUS.fromJson(jo.optJSONObject("status"));

			if (responseStatus.succeed == 1)
			{
				if (isBuyNow)
				{
					Intent it = new Intent(ProductDetailActivity.this,
							ShoppingCartActivity.class);
					ProductDetailActivity.this.startActivityForResult(it, 1);
					// 更新购物车数量
					ShoppingCartModel.getInstance().goods_num += GoodDetailDraft
							.getInstance().goodQuantity;
					good_detail_shopping_cart_num.setVisibility(View.VISIBLE);
					good_detail_shopping_cart_num.setText(ShoppingCartModel
							.getInstance().goods_num + "");
				} else
				{
					ToastView toast = new ToastView(this,
							R.string.add_to_cart_success);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					// 更新购物车数量
					ShoppingCartModel.getInstance().goods_num += GoodDetailDraft
							.getInstance().goodQuantity;
					good_detail_shopping_cart_num.setVisibility(View.VISIBLE);
					good_detail_shopping_cart_num.setText(ShoppingCartModel
							.getInstance().goods_num + "");

				}
			}

		} else if (url.endsWith(ProtocolConst.COLLECTION_CREATE))
		{
			dataModel.goodDetail.collected = 1;
			ToastView toast = new ToastView(this, R.string.collection_success);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		} else if (url.endsWith(ProtocolConst.GOODSDESC))
		{

			webView.loadDataWithBaseURL(null, dataModel.goodsWeb, "text/html",
					"utf-8", null);

		} else if (url.endsWith(ProtocolConst.COMMENTS))
		{
			if (commentModel.comment_list.size() > 0)
			{
				commentToStr(commentModel.comment_list);
				webView2.loadDataWithBaseURL(null, commentStr, "text/html",
						"utf-8", null);
				filter_comment_tabtwo.setText("评论("
						+ commentModel.comment_list.size() + ")");
				if (commentModel.paginated.more == 1)
				{
					loadmore.setVisibility(View.VISIBLE);
				} else
				{
					loadmore.setVisibility(View.GONE);
				}
				goodsComments.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						// TODO Auto-generated method stub
						goodsDetails
								.setBackgroundResource(R.drawable.detail_tab_normal);
						goodsComments
								.setBackgroundResource(R.drawable.detail_tab_selected);
						filter_detail_tabone.setTextColor(getResources()
								.getColor(R.color.title_custom));
						filter_comment_tabtwo.setTextColor(getResources()
								.getColor(R.color.title_select));
						webView.setVisibility(View.GONE);
						webView2.setVisibility(View.VISIBLE);
						fragment.setVisibility(View.GONE);
						layout_recom.setVisibility(View.GONE);
						isComment = true;
					}
				});
			} else
			{
				// null_paView.setVisibility(View.VISIBLE);
				webView2.setVisibility(View.GONE);
				loadmore.setVisibility(View.GONE);
				goodsComments.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						// TODO Auto-generated method stub
						goodsDetails
								.setBackgroundResource(R.drawable.detail_tab_normal);
						goodsComments
								.setBackgroundResource(R.drawable.detail_tab_selected);
						filter_detail_tabone.setTextColor(getResources()
								.getColor(R.color.title_custom));
						filter_comment_tabtwo.setTextColor(getResources()
								.getColor(R.color.title_select));
						webView.setVisibility(View.GONE);
						fragment.setVisibility(View.VISIBLE);
						layout_recom.setVisibility(View.GONE);
						webView2.setVisibility(View.GONE);
						isComment = true;
					}
				});
			}
			// setComment();
		}
	}

	private void commentToStr(ArrayList<COMMENTS> comment)
	{
		if (reload)
		{
			commentStr = "<style type=\"text/css\">.item{border-bottom: 1px solid #ccc;}.item .utime{float: right;}</style>";
			reload = false;
		}
		for (int i = (commentModel.pagination.page - 1)
				* commentModel.pagination.count; i < comment.size(); i++)
		{
			COMMENTS item = comment.get(i);
			commentStr += "<div class=\"item\">";
			commentStr += "<span class=\"uname\">" + item.author + "</span>";
			commentStr += "<span class=\"utime\">"
					+ TimeUtil.timeAgo(item.create) + "</span>";
			commentStr += "<p>" + item.content + "</p>";
			commentStr += "</div>";
		}
		commentStr += "";

	}

	@Override
	protected void onResume()
	{
		super.onResume();
		goodCategoryTextView.setText(getSpecificationDesc());

		if (BaolebeiManager.getUmengKey(this) != null)
		{
			MobclickAgent.onPageStart("GoodDetail");
			MobclickAgent.onResume(this, BaolebeiManager.getUmengKey(this), "");
		}

	}

	public String getSpecificationDesc()
	{
		Resources resource = (Resources) getBaseContext().getResources();
		String none = resource.getString(R.string.none);

		boolean isSelected = GoodDetailDraft.getInstance().selectedSpecification
				.size() > 0 ? true : false;

		String speciationDesc = "";

		for (int i = 0; i < dataModel.goodDetail.specification.size(); i++)
		{
			SPECIFICATION specification = dataModel.goodDetail.specification
					.get(i);
			speciationDesc += specification.name;
			speciationDesc += " : ";

			String selectedSpecificationValue = "";

			for (int k = 0; k < GoodDetailDraft.getInstance().selectedSpecification
					.size(); k++)
			{
				SPECIFICATION_VALUE localValue = GoodDetailDraft.getInstance().selectedSpecification
						.get(k);
				if (0 == specification.name
						.compareTo(localValue.specification.name))
				{
					selectedSpecificationValue += localValue.label;
					selectedSpecificationValue += "、";
				}
			}
			if (null != selectedSpecificationValue
					&& selectedSpecificationValue.length() > 0)
			{
				if (selectedSpecificationValue.endsWith("、"))
				{
					selectedSpecificationValue = selectedSpecificationValue
							.substring(0,
									selectedSpecificationValue.length() - 1);
				}
				speciationDesc += selectedSpecificationValue;
			} else
			{
				if (!isSelected)
				{

					if (0 == specification.attr_type
							.compareTo(SPECIFICATION.SINGLE_SELECT)
							&& specification.value.size() == 1)
					{
						for (int j = 0; j < specification.value.size(); j++)
						{
							SPECIFICATION_VALUE value = specification.value
									.get(j);
							speciationDesc += value.label;
							if (j != specification.value.size() - 1)
							{
								speciationDesc += "、";
							}
						}

						GoodDetailDraft.getInstance().selectedSpecification
								.add(specification.value.get(0));
					} else
					{
						speciationDesc += resource
								.getString(R.string.click_select_specification);
					}
				} else
				{
					speciationDesc += none;
				}

			}

			speciationDesc += "\n";
		}
		String num = resource.getString(R.string.amount);
		speciationDesc += num + GoodDetailDraft.getInstance().goodQuantity;
		return speciationDesc;
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		GoodDetailDraft.getInstance().clear();
	}

	@Override
	public void onRefresh(int id)
	{
		reload = true;
		if (isComment)
		{
			layout_recom.setVisibility(View.GONE);
		} else
		{
			layout_recom.setVisibility(View.VISIBLE);
			showphone();
		}
		dataModel.fetchGoodDetail(dataModel.goodId);
	}

	@Override
	public void onLoadMore(int id)
	{

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == 1)
		{
			if (ShoppingCartModel.getInstance().goods_num == 0)
			{
				good_detail_shopping_cart_num.setVisibility(View.GONE);
			} else
			{
				good_detail_shopping_cart_num.setVisibility(View.VISIBLE);
				good_detail_shopping_cart_num.setText(ShoppingCartModel
						.getInstance().goods_num + "");
			}
		} else if (requestCode == 2)
		{
			if (resultCode == Activity.RESULT_OK)
			{
				ArrayList<Integer> specIdList = new ArrayList<Integer>();
				for (int i = 0; i < GoodDetailDraft.getInstance().selectedSpecification
						.size(); i++)
				{
					SPECIFICATION_VALUE specification_value = GoodDetailDraft
							.getInstance().selectedSpecification.get(i);
					specIdList.add(Integer.valueOf(specification_value.id));
				}

				dataModel.cartCreate(dataModel.goodId, specIdList,
						GoodDetailDraft.getInstance().goodQuantity);
			}
		}
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(
				requestCode);
		if (ssoHandler != null)
		{
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		if (BaolebeiManager.getUmengKey(this) != null)
		{
			MobclickAgent.onPageEnd("GoodDetail");
			MobclickAgent.onPause(this);
		}
	}
}
